package com.xiaochong.tir.order.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.mq.util.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by org on 2018/6/11.
 * 获取人民币/美元汇率
 */
@Component
public class CnyToUsdRateComponent {
    @Value("${cnyToUsdRate.url}")
    private String cnyToUsdRate;
    @Resource
    private RedisServer redisServer;
    public String getCnyUsdRate(){
        LogTrace.beginTrace();
        String rate = "0";
        try {
            //获取指定接口数据
            String data = HttpUtils.get(cnyToUsdRate);
            if(StringUtils.isNotBlank(data)){
                JSONObject jsonObject = JSON.parseObject(data);
                Map<String,Object> rateObject = (Map<String, Object>) jsonObject.get("data");
                rate = String.valueOf(rateObject.get("rate"));
                if(StringUtils.isNotBlank(rate)){
                    redisServer.set(RedisKeysUtils.RMB_USD_RATE,rate);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("获取人民币/美元汇率",e);
        }
        return  rate;
    }
}
